<?php
if (!ob_start("ob_gzhandler")) {
  ob_start();
}

require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';
require_once 'meta.php';

session_name(SESSION);
session_start();

$logout = Utils::get_param('logout', NULL);
$url = Utils::get_param('u', NULL);
$dir = Utils::get_param('d', 'login');
$file = Utils::get_param('f', 'index');

if ($logout) {
  unset($_SESSION[IS_AUTHENTICATED]);
  header('location: ' . URL . INSTALL_DIR);
  return;
} 

if (!in_array($dir, unserialize(EXCEPTIONS)) && !isset($_SESSION[IS_AUTHENTICATED])) {
  header('location: ' . URL . INSTALL_DIR . '?u=' . urlencode(Utils::get_url()));
  return;
} 

if (in_array($dir, unserialize(EXCEPTIONS)) && isset($_SESSION[IS_AUTHENTICATED])) {
  $url = ($url) ? $url : INSTALL_HOMEPAGE;
  header('location: ' . $url);
  return;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>PZ SOFTWARE</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
    <link rel="stylesheet" type="text/css" href="../3rd_party/font-awesome/css/font-awesome.css">
    
    <link rel="stylesheet" href="skin/css/custom.css"></link>
    <link rel="stylesheet" href="skin/css/pure.css"></link>
    <link rel="stylesheet" href="skin/css/pure_tp.css"></link>
    
    <script type="text/javascript" src="../3rd_party/jquery/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="../3rd_party/jquery/jquery-ui-1.10.2.custom.min.js"></script>
    <link rel="stylesheet" href="skin/css/jquery_ui/jquery-ui-1.10.3.custom.css"></link>
    
    <script type="text/javascript" src="../3rd_party/blockUI/jquery.blockUI.js"></script>
    
    <script type="text/javascript" src="../3rd_party/noty/jquery.noty.js"></script>
    <script type="text/javascript" src="../3rd_party/noty/layouts/inline.js"></script>
    <script type="text/javascript" src="../3rd_party/noty/themes/default.js"></script>
    
    <script type="text/javascript" src="../include/js/utils.js"></script>
    <script type="text/javascript" src="../include/js/ajax.js"></script>
    <script type="text/javascript" src="../include/js/url.js"></script>
    
    <script type="text/javascript" src="../3rd_party/swfupload/swfupload.js"></script>
    <script type="text/javascript" src="../3rd_party/swfupload/swfupload.queue.js"></script>
    <script type="text/javascript" src="../include/js/upload.js"></script>
    
    <?php 
      if (file_exists("src/$dir/$file.hd.php")) {
        include "src/$dir/$file.hd.php";
      }
    ?>
  </head>

  <body class="pure-skin-mine">
    <div class="cs_header">
      <?php if (isset($_SESSION[IS_AUTHENTICATED])) { ?>
      <ul class="cs_menubar">
        <li class="cs_menu <?php if ($dir == 'systems') { ?>cs_hi<?php } ?>"><a href="?d=systems">Systems</a></li>
        <li class="cs_menu <?php if ($dir == 'plugins') { ?>cs_hi<?php } ?>"><a href="<?php if ($dir == 'systems') { ?>#<?php } else { ?>?d=plugins<?php } ?>" <?php if ($dir == 'systems') { ?>onclick="window.open('<?php echo URL . INSTALL_DIR . '?d=plugins'; ?>');"<?php } ?>>Plugins</a></li>
        <li class="cs_menu <?php if ($dir == 'themes') { ?>cs_hi<?php } ?>"><a href="<?php if ($dir == 'systems') { ?>#<?php } else { ?>?d=themes<?php } ?>" <?php if ($dir == 'systems') { ?>onclick="window.open('<?php echo URL . INSTALL_DIR . '?d=themes'; ?>');"<?php } ?>>Themes</a></li>
        <li class="cs_logout"><a href="?logout=1">Logout</a></li>
      </ul>
      <?php } ?>
    </div>
    <?php $orm = new ORM(); ?>
    <div class="cs_body"><?php include "src/$dir/$file.php"; ?></div>
    <?php $orm->close(); ?>
  </body>
</html>